package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.controller;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Medicine;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.MedicineService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 控制器类，处理与药品（Medicine）相关的HTTP请求
 */
@CrossOrigin
@Slf4j
@RestController
@ResponseBody
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    /**
     * 药品分页查询
     * @param page 页码，默认为1
     * @param pageSize 每页大小，默认为10
     * @param medicineName 药品名称，可选
     * @return 分页查询结果
     */
    @GetMapping("/medicine")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String medicineName) {
        log.info("药品分页查询：{}，{},{}", page, pageSize, medicineName);
        PageBean pageBean = medicineService.page(page, pageSize, medicineName);
        return Result.success(pageBean);
    }

    /**
     * 根据药品ID删除药品
     * @param medicineID 药品ID
     * @return 操作结果
     */
    @DeleteMapping("/medicine/{medicineID}")
    public Result delete(@PathVariable Integer medicineID) {
        log.info("根据id删除药品：{}", medicineID);
        medicineService.delete(medicineID);
        return Result.success();
    }

    /**
     * 新增药品
     * @param medicine 包含新增药品信息的对象
     * @return 操作结果
     */
    @PostMapping("/medicine")
    public Result insert(@RequestBody Medicine medicine) {
        log.info("新增药品：{}", medicine);
        medicineService.add(medicine);
        return Result.success();
    }

    /**
     * 编辑药品信息
     * @param medicine 包含编辑后的药品信息的对象
     * @return 操作结果
     */
    @PutMapping("/medicine")
    public Result update(@RequestBody Medicine medicine) {
        log.info("编辑药品：{}", medicine);
        medicineService.update(medicine);
        return Result.success();
    }
}
